const util = require('../../utils/util');
const request = require('../../utils/request')
const common = require('../../utils/common')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义头部
        navbarHeight: 0,

        //头部滚动
        scrollTop: 0,

        //banner轮播
        currentSwiper: 0,

        //选项卡
        tabActiveIndex: 0,

        bannerList: [],

        // 热销好货
        hotList: [],
        timeGoodList: [],
        timeList: [],
        //当前抢购时间
        currentTimer: '',
        // 本厂结束时间
        endTime: '',

        // 倒计时开启与否
        isCountDown: false,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            navbarHeight: util.getNavbarHeight(),
        })
        //改变头部电量条背景色
        wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: '#ffffff',
        })
        this.getData()
    },

    onShow() {
        this.getTimeList()
    },

    /**
     * 头部滚动
     */
    onPageScroll(e) {
        this.setData({
            scrollTop: e.scrollTop,
        });
    },

    /**
     * 
     * swiper内容左右滑动切换触发
     */
    swiperChange(e) {
        const current = e.detail.current;
        this.setData({
            currentSwiper: current, // 更新当前选中
        });
    },

    /**
     * 选项卡
     */
    tabClick(e) {
        const index = e.currentTarget.dataset.index;
        const item = e.currentTarget.dataset.item;
        this.setData({
            tabActiveIndex: index,
            currentTimer: item.stime
        })
        this.getTimeGoods()
    },

    // 自营首页banner,热销好货
    getData() {
        let _this = this
        request._post('/api?method=ziying.homepage', {
            store_id:0
        }).then(res => {
            _this.setData({
                bannerList: res.data.ad,
                hotList: res.data.hot
            })
        })
    },
    // 限时抢购商品列表
    getTimeGoods() {
        let _this = this
        request._post('/api?method=ziying.xianshigoods', {
            time: this.data.currentTimer
        }).then(res => {
            // console.log(res,'限时抢购商品列表')
            _this.setData({
                timeGoodList: res.data
            })

        })
    },
    // 获取限时时间段
    getTimeList() {
        let _this = this
        let newDate = new Date()
        let nowTime = newDate.getHours()
        let currentTimer = this.data.currentTimer
        let isCountDown = this.data.isCountDown
        let endTime = this.data.endTime
        request._get('/api?method=ziying.xianshitimes', {}).then(res => {
            let datas = res.data
            datas.forEach(function (obj, index) {
                if (obj.status == 1) {
                    currentTimer = obj.stime
                    isCountDown = true
                    endTime = obj.end_time
                } else {
                    currentTimer = datas[0].stime
                    if (datas[0].stime == nowTime) {
                        isCountDown = true
                        endTime = datas[0].end_time
                    }
                }
            })
            _this.setData({
                timeList: res.data,
                isCountDown,
                currentTimer,
                endTime
            })
            _this.countDown(_this.data.endTime)
            _this.getTimeGoods()
        })
    },
    // 商品详情
    toDetail(e) {
        wx.navigateTo({
            url: '/pages/home/pages/my/product-details/product-details?id=' + e.currentTarget.dataset.item.goods_id,
        })
    },
    // 倒计时
    countDown(endTime) {
        if (!endTime) {
            return
        }
        let timer = setInterval(() => {
            //截止时间
            var obj = {}
            let time = endTime
            time--;
            let now = new Date().getTime();
            let diff = parseInt(time - now / 1000);
            if (diff < 0) {
                clearInterval(timer)
                this.setData({
                    isCountDown:false
                })
            } else {
                let date = common.formattime(diff * 1000);
                this.setData({
                    hour: date.hour,
                    minute: date.minute,
                    second: date.second
                })

            }

        }, 1000)
    },
    // 分享
    onShareAppMessage: function (e) {
        let type = e.target.dataset.type
        if(type == 1) {
            return {
                title: '平台资质',
                path: '/pages/mine/pages/my/qualification/index?type=1',
              }
        } else {
            return {
                title: '优荟商城',
                path: '/pages/self-support/self-support',
              }
        }
		
    },
    toSearch() {
        wx.navigateTo({
            url: '/pages/home/pages/my/search/search',
        })
    },
    toZizhi(){
        wx.navigateTo({
            url: '/pages/mine/pages/my/qualification/index?type=1',
        })
    }


})